package sn.gandal.agriculture.agrimobile.j2me.gui;

import java.io.IOException;
import java.util.Date;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.rms.RecordStoreException;
import sn.gandal.agriculture.agrimobile.j2me.businessObjects.Membre;


/**
 *
 * @author dysow
 */
public class AddAbonneForm extends Form implements CommandListener
{

    private Display display;
    private Displayable previousScreen;
    private StringItem strId;
    private DateField dtDateInscription;
    private TextField txtPrenom;
    private TextField txtNom;
    private TextField txtPrenomMere;
    private TextField txtNomMere;
    private TextField txtPrenomPere;
    private TextField txtNomPere;
    private TextField txtNumeroTel;
    private DateField dtDateNaissance;
    private TextField txtPoidsNaissance;
    private ChoiceGroup chSexe;
    private TextField txtPrenomPayeur;
    private TextField txtNomPayeur;
    private TextField txtPhonePayeur;
    private Command cmdSave;
    private Command cmdCancel;
    public static final int MAXIMUM_SIZE = 100;//définit le nombre de caractères à mettre   
  //  private JoursDePassageForm joursDePassageForm = null;

    public AddAbonneForm(Display display, Displayable previous)
    {
	super("Ajouter un Abonné");

	this.display = display;
	this.previousScreen = previous;
	this.initialiseComponents();
	this.setCommandListener(this);
	this.initialiseCommands();
	this.addComponents();
	this.addCommands();

    }

    private void initialiseCommands()
    {
	this.cmdSave = new Command("Save", Command.OK, 1);
	this.cmdCancel = new Command("Cancel", Command.CANCEL, 2);
    }

    private void addCommands()
    {
	this.addCommand(this.cmdSave);
	this.addCommand(this.cmdCancel);
    }

    private void addComponents()
    {
	//this.append(this.dtDateInscription);
	this.append(this.strId);
	this.append(this.txtPrenom);
	this.append(this.txtNom);
	this.append(this.txtPrenomMere);
	this.append(this.txtNomMere);
	this.append(this.txtPrenomPere);
	this.append(this.txtNomPere);
	this.append(this.txtNumeroTel);
	this.append(this.dtDateNaissance);
	this.append(this.txtPoidsNaissance);
	this.append(this.chSexe);
	this.append(this.txtPrenomPayeur);
	this.append(this.txtNomPayeur);
	this.append(this.txtPhonePayeur);
    }

    private void initialiseComponents()
    {
	this.strId = new StringItem("Id*", "");
	this.dtDateInscription = new DateField("Date Inscription*", DateField.DATE);
	this.dtDateInscription.setDate(new Date());
	this.txtPrenom = new TextField("Prenom*", "", AddAbonneForm.MAXIMUM_SIZE, TextField.ANY);
	this.txtNom = new TextField("Nom*", "", AddAbonneForm.MAXIMUM_SIZE, TextField.ANY);
	this.txtPrenomMere = new TextField("Prenom Mère*", "", AddAbonneForm.MAXIMUM_SIZE, TextField.ANY);
	this.txtNomMere = new TextField("Nom Mère", "", AddAbonneForm.MAXIMUM_SIZE, TextField.ANY);
	this.txtPrenomPere = new TextField("Prenom Père", "", AddAbonneForm.MAXIMUM_SIZE, TextField.ANY);
	this.txtNomPere = new TextField("Nom Père", "", AddAbonneForm.MAXIMUM_SIZE, TextField.ANY);
	this.txtNumeroTel = new TextField("Numéro de Téléphone", "", AddAbonneForm.MAXIMUM_SIZE, TextField.NUMERIC);
	this.dtDateNaissance = new DateField("Date de Naissance*", DateField.DATE);
	this.txtPoidsNaissance = new TextField("Poids à la naissance*", "", AddAbonneForm.MAXIMUM_SIZE, TextField.DECIMAL);
	this.txtPrenomPayeur = new TextField("Prénom du Payeur*", "", AddAbonneForm.MAXIMUM_SIZE, TextField.ANY);
	this.txtNomPayeur = new TextField("Nom du Payeur*", "", AddAbonneForm.MAXIMUM_SIZE, TextField.ANY);
	this.txtPhonePayeur = new TextField("Téléphone du Payeur*", "", AddAbonneForm.MAXIMUM_SIZE, TextField.NUMERIC);

	this.initialiseSexChoiceGroup();
    }

    private void initialiseSexChoiceGroup()
    {
//	this.chSexe = new ChoiceGroup("Sexe", ChoiceGroup.EXCLUSIVE);
//	this.chSexe.append(Configuration.ABONNE_MALE, null);
//	this.chSexe.append(Configuration.ABONNE_FEMALE, null);

    }

    /**
     * Initialiser AbonneId
     * @throws RecordStoreException
     * @see java.util.Random
     */
    public void initialiseAbonneId() throws RecordStoreException, IOException
    {
//	IBusinessServices businessServicesImpl = BusinessServicesImpl.getInstance();
//	int identifiantId = businessServicesImpl.getAgentDePeseeIdentifiant();
//
//	String abonne4Code = String.valueOf(businessServicesImpl.generateNewAbonneId());
//        String abonne8Code=identifiantId+abonne4Code;
//
//        System.out.println("L'id generé : "+abonne4Code);
//
//	 this.strId.setText(abonne8Code);//ADD
	
    }

    private void sauvegarderAction()
    {

	ControleFormsUtil controleFormsUtil = new ControleFormsUtil();


	String telephonePayeur = this.txtPhonePayeur.getString();

	String telephone = this.txtNumeroTel.getString();

	String validerChamps = controleFormsUtil.controleChampsAbonne(telephonePayeur, telephone);

	if (validerChamps.length() > 0)
	{
	    this.display.setCurrent(new ErrorsForm(validerChamps), this);
	}
	else
	{
	    int idAbonneInt = Integer.parseInt(this.strId.getText());
	    Date dateInscription = this.dtDateInscription.getDate();
	    String prenom = this.txtPrenom.getString();
	    String nom = this.txtNom.getString();
	    String prenomMere = this.txtPrenomMere.getString();
	    String nomMere = this.txtNomMere.getString();
	    String prenomPere = this.txtPrenomPere.getString();
	    String nomPere = this.txtNomPere.getString();
	    Date dateNaissance = this.dtDateNaissance.getDate();
	    String poidsNaissance = this.txtPoidsNaissance.getString();
	    String sexe = this.chSexe.getString(this.chSexe.getSelectedIndex());
	    String prenomPayeur = this.txtPrenomPayeur.getString();
	    String nomPayeur = this.txtNomPayeur.getString();
	    long telephonePayeurLong = Long.parseLong(this.txtPhonePayeur.getString());

	    Membre abonne = new Membre(dateInscription, idAbonneInt, prenom, nom, prenomMere, nomMere, prenomPere, nomPere, telephone,
		    dateNaissance, poidsNaissance, sexe, nomPayeur, prenomPayeur, telephonePayeurLong);

	    String validate = controleFormsUtil.controleSauvegardeAbonne(abonne);

	    if (validate.length() > 0)
	    {
		this.display.setCurrent(new ErrorsForm(validate), this);
	    }
	    else
	    {
		//this.display.setCurrent(this.getJoursDePassageForm(abonne));
	    }
	}

    }

    private void cancelAction()
    {
	this.display.setCurrent(this.previousScreen);
    }

    public void commandAction(Command c, Displayable d)
    {
	if (c.equals(this.cmdSave))
	{
	    this.sauvegarderAction();
	}
	else if (c.equals(this.cmdCancel))
	{
	    this.cancelAction();
	}
    }

 
}
